<template>
  <div class="product-detail-container">
    <el-card class="detail-card">
      <el-page-header @back="goBack" content="商品详情"></el-page-header>

      <div class="product-detail-content">
        <el-row :gutter="30">
          <!-- 左侧图片展示 -->
          <el-col :span="10">
            <div class="image-gallery">
              <img :src="currentImage" class="main-image" alt="商品图片">
              <div class="thumb-images">
                <img v-for="(img, index) in productInfo.images" :key="index"
                     :src="img" class="thumb-image"
                     :class="{ active: currentImage === img }"
                     @click="currentImage = img"
                     alt="缩略图">
              </div>
            </div>
          </el-col>

          <!-- 右侧商品信息 -->
          <el-col :span="14">
            <div class="product-info">
              <div class="product-header">
                <h1 class="product-name">{{ productInfo.name }}</h1>
                <el-tag v-if="productInfo.tag" type="danger">{{ productInfo.tag }}</el-tag>
              </div>

              <p class="product-subtitle">{{ productInfo.subtitle }}</p>

              <div class="price-section">
                <div class="price-row">
                  <span class="label">价格：</span>
                  <span class="current-price">¥{{ productInfo.price }}</span>
                  <span class="original-price" v-if="productInfo.originalPrice">
                    ¥{{ productInfo.originalPrice }}
                  </span>
                </div>
                <div class="sales-row">
                  <span>销量：{{ productInfo.sales }}</span>
                  <span>库存：{{ productInfo.stock }}件</span>
                </div>
              </div>

              <el-divider></el-divider>

              <!-- 规格选择 -->
              <div class="spec-section">
                <div class="spec-item" v-if="productInfo.specs">
                  <span class="spec-label">规格：</span>
                  <el-radio-group v-model="selectedSpec">
                    <el-radio-button v-for="spec in productInfo.specs" :key="spec" :label="spec">
                      {{ spec }}
                    </el-radio-button>
                  </el-radio-group>
                </div>

                <div class="spec-item">
                  <span class="spec-label">数量：</span>
                  <el-input-number v-model="quantity" :min="1" :max="productInfo.stock"
                                   @change="handleQuantityChange"></el-input-number>
                </div>
              </div>

              <el-divider></el-divider>

              <!-- 操作按钮 -->
              <div class="action-buttons">
                <el-button type="warning" size="large" icon="el-icon-shopping-cart-2"
                           @click="handleAddToCart">
                  加入购物车
                </el-button>
                <el-button type="primary" size="large" icon="el-icon-goods"
                           @click="handleBuyNow">
                  立即购买
                </el-button>
                <el-button size="large" icon="el-icon-star-off">
                  收藏
                </el-button>
              </div>

              <!-- 服务保障 -->
              <div class="service-section">
                <h4>服务保障</h4>
                <el-row :gutter="10">
                  <el-col :span="12">
                    <div class="service-item">
                      <i class="el-icon-circle-check"></i>
                      <span>7天无理由退换</span>
                    </div>
                  </el-col>
                  <el-col :span="12">
                    <div class="service-item">
                      <i class="el-icon-circle-check"></i>
                      <span>正品保障</span>
                    </div>
                  </el-col>
                  <el-col :span="12">
                    <div class="service-item">
                      <i class="el-icon-circle-check"></i>
                      <span>极速发货</span>
                    </div>
                  </el-col>
                  <el-col :span="12">
                    <div class="service-item">
                      <i class="el-icon-circle-check"></i>
                      <span>售后无忧</span>
                    </div>
                  </el-col>
                </el-row>
              </div>
            </div>
          </el-col>
        </el-row>

        <!-- 商品详情标签页 -->
        <el-card class="detail-tabs-card">
          <el-tabs v-model="activeTab">
            <el-tab-pane label="商品详情" name="detail">
              <div class="detail-content" v-html="productInfo.detailContent"></div>
            </el-tab-pane>
            <el-tab-pane label="规格参数" name="params">
              <el-descriptions :column="2" border>
                <el-descriptions-item v-for="(value, key) in productInfo.params" :key="key" :label="key">
                  {{ value }}
                </el-descriptions-item>
              </el-descriptions>
            </el-tab-pane>
            <el-tab-pane label="用户评价" name="reviews">
              <div class="review-list">
                <div class="review-item" v-for="review in reviews" :key="review.id">
                  <div class="review-header">
                    <el-avatar :src="review.avatar">{{ review.username }}</el-avatar>
                    <div class="review-user-info">
                      <div class="username">{{ review.username }}</div>
                      <el-rate v-model="review.rating" disabled show-score text-color="#ff9900"></el-rate>
                    </div>
                    <div class="review-date">{{ review.date }}</div>
                  </div>
                  <div class="review-content">{{ review.content }}</div>
                </div>
              </div>
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'ProductDetail',
  data () {
    return {
      productInfo: {
        id: 1,
        name: '高级狗粮5kg',
        subtitle: '营养均衡，适合成犬，天然健康配方',
        price: 199,
        originalPrice: 259,
        sales: 1230,
        stock: 500,
        tag: '热销',
        specs: ['5kg', '10kg', '15kg'],
        images: [],
        detailContent: '<p>这是商品的详细介绍内容...</p>',
        params: {
          '品牌': '皇家',
          '产地': '法国',
          '适用对象': '成犬',
          '主要成分': '鸡肉、谷物、维生素',
          '保质期': '18个月',
          '储存方法': '阴凉干燥处'
        }
      },
      currentImage: '',
      selectedSpec: '5kg',
      quantity: 1,
      activeTab: 'detail',
      reviews: []
    }
  },
  created () {
    this.loadProductDetail()
    this.loadReviews()
  },
  methods: {
    loadProductDetail () {
      const productId = this.$route.query.id
      console.log('加载商品详情', productId)
      this.currentImage = this.productInfo.images[0]
      // 这里将来对接接口 GET /api/product/detail/{id}
    },
    loadReviews () {
      // 模拟评价数据
      this.reviews = [
        { id: 1, username: '用户A', avatar: '', rating: 5, date: '2024-01-15', content: '非常好的狗粮，我家狗狗很喜欢吃！' },
        { id: 2, username: '用户B', avatar: '', rating: 4, date: '2024-01-14', content: '质量不错，价格实惠，会回购的。' },
        { id: 3, username: '用户C', avatar: '', rating: 5, date: '2024-01-13', content: '包装完好，物流很快，狗狗吃了之后毛色都变好了。' }
      ]
    },
    handleQuantityChange (value) {
      console.log('数量变更', value)
    },
    handleAddToCart () {
      // 这里将来对接接口 POST /api/cart/add
      this.$message.success(`已将 ${this.quantity} 件商品加入购物车`)
    },
    handleBuyNow () {
      // 这里将来对接接口 POST /api/order/create
      this.$confirm('确认立即购买吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success('购买成功！')
        this.$router.push('/user/user-center')
      })
    },
    goBack () {
      this.$router.back()
    }
  }
}
</script>

<style scoped>
.product-detail-container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.detail-card {
  margin-bottom: 20px;
}

.product-detail-content {
  margin-top: 30px;
}

.image-gallery {
  position: sticky;
  top: 20px;
}

.main-image {
  width: 100%;
  height: 450px;
  object-fit: cover;
  border-radius: 8px;
  margin-bottom: 10px;
}

.thumb-images {
  display: flex;
  gap: 10px;
}

.thumb-image {
  width: calc(25% - 7.5px);
  height: 100px;
  object-fit: cover;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
  border: 2px solid transparent;
}

.thumb-image:hover, .thumb-image.active {
  border-color: #409EFF;
  opacity: 0.8;
}

.product-info {
  padding: 10px;
}

.product-header {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 10px;
}

.product-name {
  font-size: 24px;
  margin: 0;
  color: #333;
}

.product-subtitle {
  color: #999;
  margin-bottom: 20px;
}

.price-section {
  background: linear-gradient(135deg, #fff5f5 0%, #ffe9e9 100%);
  padding: 20px;
  border-radius: 8px;
}

.price-row {
  display: flex;
  align-items: baseline;
  gap: 15px;
  margin-bottom: 10px;
}

.current-price {
  font-size: 32px;
  color: #f56c6c;
  font-weight: bold;
}

.original-price {
  font-size: 18px;
  color: #999;
  text-decoration: line-through;
}

.sales-row {
  display: flex;
  gap: 30px;
  color: #666;
  font-size: 14px;
}

.spec-section {
  margin: 20px 0;
}

.spec-item {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.spec-label {
  width: 80px;
  color: #666;
}

.action-buttons {
  display: flex;
  gap: 15px;
  margin: 30px 0;
}

.action-buttons .el-button {
  flex: 1;
}

.service-section {
  background-color: #f9f9f9;
  padding: 20px;
  border-radius: 8px;
}

.service-section h4 {
  margin: 0 0 15px 0;
  color: #333;
}

.service-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  color: #666;
}

.service-item i {
  color: #67C23A;
  font-size: 18px;
}

.detail-tabs-card {
  margin-top: 30px;
}

.detail-content {
  line-height: 1.8;
  color: #666;
}

.review-list {
  max-width: 800px;
}

.review-item {
  padding: 20px 0;
  border-bottom: 1px solid #eee;
}

.review-header {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 10px;
}

.review-user-info {
  flex: 1;
}

.username {
  font-weight: bold;
  margin-bottom: 5px;
}

.review-date {
  color: #999;
  font-size: 12px;
}

.review-content {
  margin-left: 55px;
  color: #666;
  line-height: 1.6;
}
</style>
